/**
 * Created by TianJing on 2017/3/20.
 */
var SearchList=function () {
    this.dateRange=$("#date-range");//定义日期选择插件
    this.searchBtn=$(".searchButton");
    this.resetSearchBtn=$(".resetForm");
    this.chosenSelects=$('.result-selected');

    !!this.dateRange.length?this.dateRange.datepicker({ keyboardNavigation: false, forceParse: false, autoclose: true }):null;//初始化日期选择插件
};
SearchList.prototype={
    getDataList:function (dataTable) {
        /**
         * 绑定搜素按钮的点击事件、触发搜索动作。
         * @param  {[type]} ) {                   oTable.fnClearTable();        return false;    } [description]
         * @return {[type]}   [description]
         */
        var self=this;
        self.searchBtn.click(function() {
            dataTable.fnClearTable();
            return false;
        });
    },
    resetSearch:function (dataTable) {
        /**
         * 绑定重置表单按钮的点击事件、触发重置表单控件。
         */
        var self=this;
        $chosenSingle=$('.chosen-single');
        self.resetSearchBtn.click(function () {
            self.chosenSelects.removeClass('result-selected');
            $chosenSingle.children('span').html('请选择...');
            // dataTable.fnClearTable();
        });
        return false;
    },
    createDataListByInput:function (options) {
        var self=this;
        var dataListPanel=$(options.dataListPanel);
        var searchInput=$(options.searchInput);
        var searchInputId=$(options.searchInputId);
        var listUrl=options.listUrl;
        var listClick=options.listClick;
        var searchJSON={pageNum:"1",numPerPage:"10",status:"AVAILABLE"};
        var timer="";
        var domList=[];
        searchInput.off("keyup").on("keyup",function (ev) {
            var $this=$(this);
            var searchWord=$this.val();
            searchJSON[$this.attr("name")]=searchWord;
            !!searchInputId.length?searchInputId.val(""):null;
            clearTimeout(timer);
            if(searchWord===""){
                dataListPanel.html("").hide();
            }else {
                timer=setTimeout(function () {
                    self.getDataListByUrl(listUrl,searchJSON,dataListPanel,"companyName",domList);
                },200)
            }
            ev.stopPropagation();
        });
        searchInput.on("blur",function (ev) {
            setTimeout(function () {
                dataListPanel.hide()
            },200);
        });
        searchInput.off("focus").on("focus",function (ev) {
            dataListPanel.show();
        });
        dataListPanel.off("click").on("click","li",function () {
            var $this=$(this);
            var selfText=$this.attr("data-value");
            var selfId=$this.attr("data-id");
            !!searchInputId.length?searchInputId.val(selfId):null;
            searchInput.val(selfText) && dataListPanel.hide();
            !!listClick && $.isFunction(listClick) && listClick($this);
        });
    },
    getDataListByUrl:function (url,sendData,dataListPanel,textKey,domList) {
        $.post(url,sendData).success(function (data) {
            var record=common.transformData(data);
            domList=[];
            $.each(record,function (idx, ele) {
                domList.push('<li data-id="'+ele.id+'" data-value="'+ele[textKey]+'">'+ele[textKey]+'</li>');
            });
            dataListPanel.html(domList.join("")).show();
        });
    }
};

window.searchList= new SearchList();
